package com.shi.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.shi.util.EncodeUtil;
import com.shi.util.TuPanel;

import cn.hutool.core.io.file.FileAppender;

import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.SwingConstants;
import java.awt.SystemColor;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

public class EncodeJFrame extends JFrame {

	private TuPanel contentPane; // 图片类 TuPanel - 背景图片工具类
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane_1;
	private JTextPane textPane;

/*	*//**
	 * Create the frame.
	 *//*
	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EncodeJFrame frame = new EncodeJFrame();
					// setVisible 设置窗口是否显示 默认为false
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/
	public EncodeJFrame() {
	
		// setDefaultCloseOperation 设置关闭按钮 在点击关闭按钮的同时进行什么操作
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(455, 200, 608, 473);
		setLocationRelativeTo(null);

		ImageIcon img=new ImageIcon(LoginJFrame.class.getResource("/com/shi/resources/conImage.jpg"));
		setIconImage(img.getImage());

		// 背景图片
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) (screen.getWidth() - this.getWidth()) / 2;
		int y = (int) (screen.getHeight() - this.getHeight()) / 2;
		this.setLocation(x, y);

		contentPane = new TuPanel();
		contentPane.tu  = LoginJFrame.class.getResource("/com/shi/resources/background.jpg");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		btnNewButton = new JButton("加密");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

	
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				
				fileChooser.showOpenDialog(null);
			
				File file = fileChooser.getSelectedFile();

				try {
			
				if(file == null ){
				
					JOptionPane.showMessageDialog(null, "取消");
					return;
				}
				
				List<String> bhstringList = new ArrayList<String>();
				bhstringList.add("C:\\Windows");
				bhstringList.add("C:\\Users");
				bhstringList.add("C:\\Program Files");
				bhstringList.add("C:\\Program Files (x86)");
			
			
				String bhfileNname = file.getAbsolutePath();
				
				for(String s : bhstringList){
					if(bhfileNname.contains(s)){
						
						 JOptionPane.showMessageDialog(null,"当前选中文件目录为["+ bhfileNname+"]未保护数据安全包含以下文件目录不可加密["+String.join(";", bhstringList)+"]");
						return;
					}
				}
			
				
				
				//隐藏文件根路径追加存储到file.properties中
				EncodeUtil.appenderFileName(file);
			
				}catch (Exception e1) {
					
					 JOptionPane.showMessageDialog(null, "存储加密记录异常:"+e1.getMessage());
					// TODO Auto-generated catch block
					e1.printStackTrace();
					System.exit(1000);
				}
				
				try{
				//加密文件
				Boolean flag = EncodeUtil.encode(file);
					
		
				} catch (Exception e1) {
					
					 JOptionPane.showMessageDialog(null, "加密异常:"+e1.getMessage());
					// TODO Auto-generated catch block
					e1.printStackTrace();
					System.exit(1000);
				}
				
				//隐藏文件
				if(!file.isDirectory()){
				
					try {
						
					    //  当前文件所在路径
				          String partdir = file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("\\")+1);
				          
				          String fileName = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("\\")+1);  
				          
				            //  文件名称
				          
					
						EncodeUtil.exeCmd("attrib +s +h ".concat("\"".concat(partdir).concat(URLEncoder.encode( EncodeUtil.aes.encryptBase64(fileName),"utf-8")+"\"")));
					
				
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						 JOptionPane.showMessageDialog(null, "获取文件权限异常");
						System.exit(1000);
					}
				}else{
					
		
				EncodeUtil.exeCmd("attrib +s +h ".concat("\""+file.getAbsolutePath()+"\""));
				}
				
				JOptionPane.showMessageDialog(null, "完成！");
				
				//刷新历史文件记录
					textPane.setText(String.join("\n", EncodeUtil.getHistoryFileName()));
					textPane.repaint();
				
	
				
			}
		});
		btnNewButton.setBounds(14, 13, 134, 41);
		contentPane.add(btnNewButton);
		
		btnNewButton_1 = new JButton("解密所有文件");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnNewButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				 try {
		
				
				//获取历史加密文件根路径
				List<String> fileNames = EncodeUtil.getHistoryFileName();

				for(String file: fileNames){

				
					//去除隐藏属性
					if(file.contains(".")){

					    //  当前文件所在路径
				        String partdir = file.substring(0,file.lastIndexOf("\\")+1);
				          
				        String fileName = file.substring(file.lastIndexOf("\\")+1);  
				        String jmFilename = partdir.concat(URLEncoder.encode( EncodeUtil.aes.encryptBase64(fileName),"utf-8"));
				        
				    	EncodeUtil.exeCmd("attrib -s -h ".concat("\"".concat(jmFilename)+"\""));
						EncodeUtil.decode(new File(jmFilename));
					
						
						
				
					
						 
					}else{
						
						//解密文件
						 EncodeUtil.exeCmd("attrib -s -h ".concat("\""+file+"\""));
						 EncodeUtil.decode(new File(file));
						
					
			
					}
				 
				}
				//重写历史文件
				EncodeUtil.overwritegetHistoryFileName();
				JOptionPane.showMessageDialog(null, "解密文件夹数量:"+fileNames.size());
				
				//刷新历史文件记录
				textPane.setText(String.join("\n", EncodeUtil.getHistoryFileName()));

				textPane.repaint();
			
		
				 } catch (Exception e2) {
					 
					 JOptionPane.showMessageDialog(null, "解密异常:"+e2.getMessage());
						// TODO Auto-generated catch block
						e2.printStackTrace();
						System.exit(1000);
				}
			}
		});
		btnNewButton_1.setBounds(172, 13, 134, 41);
		contentPane.add(btnNewButton_1);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(14, 113, 562, 300);
		
		contentPane.add(scrollPane);
		
		textPane = new JTextPane();
		textPane.setEditable(false);

		try {
			textPane.setText(String.join("\n", EncodeUtil.getHistoryFileName()));
		} catch (Exception e1) {
			 JOptionPane.showMessageDialog(null, "获取文件权限异常");
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.exit(1000);
		}
	
		scrollPane.setViewportView(textPane);
		
		
		
		JLabel lblNewLabel = new JLabel("加密情况");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 17));
		lblNewLabel.setBackground(Color.WHITE);
		lblNewLabel.setBounds(14, 82, 72, 18);
		contentPane.add(lblNewLabel);


	}
}
